#include "uanalyserrenderer.h"
#include "uanalyserchannel.h"
#include <math.h>

typedef struct {
    QString name;
    QColor color;
} colorCombo;

extern colorCombo traceColors[];


/*******************************************
 * uAnalyserRenderer
 *******************************************/

uAnalyserRenderer::uAnalyserRenderer(QObject *parent) :
    QGraphicsScene(parent)
{
    gridColor = QColor(128, 192, 255);
    axesColor = QColor(0, 0, 0);
    grid_size = 40;

    for (qreal x=-200; x<=200; x+=grid_size)
        gridLines << new QGraphicsLineItem ( x, -200, x, 200);
    for (qreal y=-200; y<=200; y+=grid_size)
        gridLines << new QGraphicsLineItem ( -200, y, 200, y);

    axesLines << new QGraphicsLineItem ( 0, -200, 0, 200);
    axesLines << new QGraphicsLineItem ( -200, 0, 200, 0);
    for (qreal x=-200; x<=200; x+=grid_size)
        axesLines << new QGraphicsLineItem ( x, -3, x, 3);
    for (qreal y=-200; y<=200; y+=grid_size)
        axesLines << new QGraphicsLineItem ( -3, y, 3, y);
    for (int i=0; i < gridLines.size(); i++)
    {
        gridLines.at(i)->setPen(QPen(gridColor, 0, Qt::DashLine));
        gridLines.at(i)->setVisible(false);
        addItem(gridLines.at(i));
    }
    for (int i=0; i < axesLines.size(); i++)
    {
        axesLines.at(i)->setPen(QPen(axesColor, 0, Qt::SolidLine));
        axesLines.at(i)->setVisible(false);
        addItem(axesLines.at(i));
    }

}

void uAnalyserRenderer::addTrace(uAnalyserChannelTrace* channel_trace)
{
    channel_trace->setPos(0, 0);
    addItem(channel_trace);
}

void uAnalyserRenderer::removeTrace(uAnalyserChannelTrace* channel_trace)
{
    removeItem(channel_trace);
}

void uAnalyserRenderer::setAxesVisible(bool v)
{
    for (int i=0; i < axesLines.size(); i++)
        axesLines.at(i)->setVisible(v);
    update();
}

void uAnalyserRenderer::setGridVisible(bool v)
{
    for (int i=0; i < gridLines.size(); i++)
        gridLines.at(i)->setVisible(v);
    update();
}

